//
//  HZOwnCellView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/20.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SnapKit

class HZOwnCellView: UIView {
    
    
    
    //定义约束对象
    var constra:Constraint?
    
    
    
    var cellVM:HZHomeCellVm?{
    
       //获取数组并赋值
        didSet{
            
            icon.sd_setImageWithURL(NSURL(string: cellVM?.cellModel?.user?.profile_image_url ?? ""))
            
            nameLabel.text = cellVM?.cellModel?.user?.name
            
            created_at.text = cellVM?.rightTime
            source.text = cellVM?.cellModel?.source
            
            textLabel.attributedText = cellVM?.ownAttributedStrinng
            
            vipImg.image = cellVM?.vipImg
            
            
            
            //更新配图区域的大小
            updateImg()
            
        }

    
    
    }
    
    //更新配图区域的大小
    private func updateImg(){
        
        //先清除约束
        constra?.uninstall()
        
        
        //如果有配图
        if let pic_urls = cellVM?.cellModel?.pic_urls where  pic_urls.count > 0{
            
            //显示出来
            iconImg.hidden = false
            
            var size:CGSize
            
            //判断是单图尺寸还是多图的
            if cellVM?.cellModel?.pic_urls?.count == 1{
                
                size = cellVM?.oneImgSize ?? CGSizeZero
            }else{
                
                size = cellVM?.imgSize ?? CGSizeZero
            }
            
            //赋值
            iconImg.dataInfo = (pic_urls,size)
            
            
            self.snp_updateConstraints { (make) -> Void in
                constra = make.bottom.equalTo(iconImg.snp_bottom).offset(HZSpacing).constraint
            }
            
            
            
        } else{
            
            
            //显示隐藏
            iconImg.hidden =  true
            
            self.snp_updateConstraints { (make) -> Void in
                constra = make.bottom.equalTo(textLabel.snp_bottom).offset(HZSpacing).constraint
            }
            
            
        }
    }

    


   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //添加子控件
    func setupUI(){
        
        //添加到视图上
        addSubview(nameLabel)
        addSubview(icon)
        addSubview(source)
        addSubview(created_at)
        addSubview(textLabel)
        addSubview(vipImg)
        
         addSubview(iconImg)
        
        
        //约束
        icon.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(HZSpacing)
            make.width.height.equalTo(HZIconSize)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(icon)
            make.leading.equalTo(icon.snp_trailing).offset(HZSpacing)
        }
        
        vipImg.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.top.equalTo(nameLabel)
        }
        
        created_at.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(icon)
            make.leading.equalTo(icon.snp_trailing).offset(HZSpacing)
        }
        
        source.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(created_at.snp_trailing).offset(HZSpacing)
            make.top.equalTo(created_at)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(icon.snp_bottom).offset(HZSpacing)
            make.leading.equalTo(HZSpacing)
            make.trailing.equalTo(-HZSpacing)
//
        }
        
        //约束配图
        iconImg.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.leading.equalTo(HZSpacing)
            
            //定死了高
            constra = make.height.width.equalTo(50).constraint
        }
        
        self.snp_makeConstraints { (make) -> Void in
            constra = make.bottom.equalTo(iconImg.snp_bottom).offset(HZSpacing).constraint
        }

        
        
        
    }
    
    
    //懒加载配图区域
    lazy var iconImg:HZImageCollView = {
        
        let iconIme = HZImageCollView.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        return iconIme
    }()
    
    
   
    
    //懒加载子控件
    
    lazy var vipImg:UIImageView = {
        
       let vipImg = UIImageView()
        
        vipImg.image = UIImage(named: "common_icon_membership_expired")
        
        //和图片一样大小
        vipImg.sizeToFit()
        
        return vipImg
    }()
    
    lazy var textLabel:YYLabel = {
       
        let textLabel = YYLabel()
        textLabel.textColor = UIColor.darkGrayColor()
        textLabel.font = UIFont.systemFontOfSize(HZNameTextFont)
        textLabel.preferredMaxLayoutWidth = screenWidth-2*HZSpacing
        textLabel.numberOfLines = 0
        
        return textLabel
    }()
    
    
    lazy var source:UILabel = {
        
        let source = UILabel(color: UIColor.darkGrayColor(), font: HZTimeTextFont, title: "")
        
        return source
    }()
    
    lazy var created_at:UILabel = {
        
        let created_at = UILabel(color: UIColor.orangeColor(), font: HZTimeTextFont, title: "")
        
        return created_at
    }()
    
    
    lazy var nameLabel:UILabel = {
        
       let nameLabel = UILabel(color: UIColor.darkGrayColor(), font: HZNameTextFont, title: "")
        
        return nameLabel
    }()
    
    
    lazy var icon:UIImageView = {
        
        let icon = UIImageView()
        
        icon.image = UIImage(named:"avatar_default_small")
        
        return icon
    }()
    
}
